import { ItemStack, world } from "@minecraft/server";
import {
  clearEffect,
  EffectGroups,
  FoodItem,
  getEquipmentItem,
  giveItem,
  initializeMod,
  pushLore,
  Quest,
  QuestBook,
  setEquipmentItem,
  ToolTag,
  WeaponTag,
} from "lazuli-mc";

initializeMod(
  "stonecraft",
  {
    name: "Stone Craft",
  },
  {
    questNameSpace: "stonecraft",
  }
);

const TOOL_TAG = new ToolTag("stonecraft:custom_tool");
const WEAPON_TAG = new WeaponTag("stonecraft:custom_weapon");
const BLINDNESS_POTION = new FoodItem("stonecraft:blindness_potion", [
  { effectType: "blindness", duration: 400 },
]);
const LONGER_BLINDNESS_POTION = new FoodItem(
  "stonecraft:longer_blindness_potion",
  [{ effectType: "blindness", duration: 800 }]
);

/**
 * 监听物品食用事件
 */
world.afterEvents.itemCompleteUse.subscribe((event) => {
  const PLAYER = event.source;
  switch (event.itemStack.typeId) {
    case "stonecraft:stone_apple":
      clearEffect(PLAYER, EffectGroups.bad);
      break;
    case "stonecraft:stone_bread":
      PLAYER.addEffect("resistance", 2000); // 100s 抗性提升
      break;
    case "stonecraft:stone_potato":
      PLAYER.addEffect("speed", 600, { amplifier: 1 }); //30s 速度II
      break;
    case "stonecraft:stone_carrot":
      PLAYER.addEffect("night_vision", 600, { amplifier: 1 }); //30s 夜视II
      break;
    case "stonecraft:stone_melon":
      PLAYER.addEffect("regeneration", 600, { amplifier: 1 }); //30s 生命恢复II
      break;
    case "stonecraft:stone_kelp":
      PLAYER.addEffect("water_breathing", 600); //30s 水下呼吸
      break;
    case "stonecraft:stone_hodgepodge":
      //移除所有负面效果，添加30s的 抗性提升；速度II；夜视II；生命恢复II；水下呼吸
      clearEffect(PLAYER, EffectGroups.bad);
      PLAYER.addEffect("resistance", 2000);
      PLAYER.addEffect("speed", 600, { amplifier: 1 });
      PLAYER.addEffect("night_vision", 600, { amplifier: 1 });
      PLAYER.addEffect("regeneration", 600, { amplifier: 1 });
      PLAYER.addEffect("water_breathing", 600);
      break;
    default:
      break;
  }
});

/**
 * 监听实体击打事件
 */
world.afterEvents.entityHitEntity.subscribe((event) => {
  const [ATTACKER, HITER, ITEM] = [
    event.damagingEntity,
    event.hitEntity,
    getEquipmentItem(event.damagingEntity),
  ];
  /**
   * 特殊的石剑-5s失明/5s凋零/生命恢复IV 30刻
   */
  if (ITEM?.getDynamicProperty("stonecraft:blindness")) {
    HITER.addEffect("blindness", 100);
  }
  if (ITEM?.getDynamicProperty("stonecraft:wither")) {
    HITER.addEffect("wither", 100);
  }
  if (ITEM?.getDynamicProperty("stonecraft:regeneration")) {
    HITER.addEffect("regeneration", 30, { amplifier: 3 });
  }
  /**
   * 终极压缩石剑相关：
   * lv1-5s 缓慢X
   * lv2-7s 缓慢X
   * lv3-10s 缓慢X；凋零V、3s 力量V(给自己)
   */
  switch (ITEM?.typeId) {
    case "stonecraft:uc_stone_sword":
      HITER.addEffect("slowness", 100, { amplifier: 4, showParticles: false });
      break;
    case "stonecraft:uc2_stone_sword":
      HITER.addEffect("slowness", 140, { amplifier: 4, showParticles: false });
      break;
    case "stonecraft:uc3_stone_sword":
      HITER.addEffect("slowness", 200, { amplifier: 4, showParticles: false });
      HITER.addEffect("wither", 200, { amplifier: 4, showParticles: false });
      ATTACKER.addEffect("strength", 60, {
        amplifier: 4,
        showParticles: false,
      });
      break;
    default:
      break;
  }
});

world.afterEvents.itemUseOn.subscribe((event) => {
  const [BLOCK, ITEM, PLAYER] = [event.block, event.itemStack, event.source];
  if (BLOCK.typeId === "stonecraft:stone_smithing_table") {
    let dropItem: ItemStack;
    // 当方块未填充物品的事件
    if (!BLOCK.permutation.getState("stonecraft:filled_item")) {
      console.warn("Table wasn't filled items.");
      // 用stonecraft:can_smithing标签判断是否可以锻造
      if (ITEM.hasTag("stonecraft:can_smithing")) {
        setEquipmentItem(PLAYER);
        BLOCK.setPermutation(
          BLOCK.permutation.withState("stonecraft:filled_item_id", ITEM.typeId)
        );
        BLOCK.setPermutation(
          BLOCK.permutation.withState("stonecraft:filled_item", true)
        );
        PLAYER.onScreenDisplay.setActionBar({
          translate: "stonecraft.message.input",
        });
      } else {
        PLAYER.onScreenDisplay.setActionBar({
          translate: "stonecraft.message.input_not_acceptable",
        });
      }
      // 当方块填充物品时的事件
    } else {
      console.warn("Table was filled items.");
      dropItem = new ItemStack(
        BLOCK.permutation.getState("stonecraft:filled_item_id") as string
      );
      // 失明加成
      if (ITEM.hasTag("stonecraft:blindness")) {
        dropItem.setDynamicProperty("stonecraft:blindness", true);
        giveItem([PLAYER], pushLore("§d攻击加成-失明", dropItem));
        BLOCK.setPermutation(
          BLOCK.permutation.withState("stonecraft:filled_item", false)
        );
        PLAYER.sendMessage({ translate: "stonecraft.message.done" });
        // 凋零加成
      } else if (ITEM.hasTag("stonecraft:wither")) {
        dropItem.setDynamicProperty("stonecraft:wither", true);
        giveItem([PLAYER], pushLore("§d攻击加成-凋零", dropItem));
        BLOCK.setPermutation(
          BLOCK.permutation.withState("stonecraft:filled_item", false)
        );
        PLAYER.sendMessage({ translate: "stonecraft.message.done" });
        // 再生加成
      } else if (ITEM.hasTag("stonecraft:regeneration")) {
        dropItem.setDynamicProperty("stonecraft:regeneration", true);
        giveItem([PLAYER], pushLore("§d攻击加成-再生", dropItem));
        BLOCK.setPermutation(
          BLOCK.permutation.withState("stonecraft:filled_item", false)
        );
        PLAYER.sendMessage({ translate: "stonecraft.message.done" });
      } else {
        PLAYER.onScreenDisplay.setActionBar({
          translate: "stonecraft.message.input_not_acceptable",
        });
      }
    }
  }
});

world.afterEvents.playerBreakBlock.subscribe((event) => {
  const BLOCK = event.block;
  if (
    BLOCK.typeId === "stonecraft:stone_smithing_table" &&
    BLOCK.permutation.getState("stonecraft:filled_item")
  ) {
    BLOCK.dimension.spawnItem(
      new ItemStack(
        BLOCK.permutation.getState("stonecraft:filled_item_id") as string
      ),
      BLOCK.location
    );
  }
});

const TEST_Q = new Quest("test", "测试", "测试", {
  condition: {
    playerXpLevel: 1,
  },
  award: {
    exp: 4,
  },
});

const TEST_QB = new QuestBook("stonecraft:test_book", "测试书籍", "测试描述", {
  quests: [TEST_Q],
});
